#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : 全球法律在线项目                #
#                                                                   #
#                   @File Name    : datefunction.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2020-12-17 10:29                 #
#                                                                   #
#                   @Last Update  : 2020-12-17 10:29                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:Timefunction是用于处理时间的类                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
import time


class Timefunction:
    # 日期处理的类
    def strpdatetime(self, timestr):
        """
        将字符串中的时间转为Y-m-d的标准格式
        import: timestr:一个日期格式字符串
        return: timedate:标准的Y-m-d日期字符串
        """
        timedate = ''
        if not timestr:
            return timedate
        try:
            # 时间字符串格式为Nov 20, 2020
            timedate = time.strptime(timestr, "%b %d, %Y")
        except:
            try:
                # 时间字符串格式为20 Nov 2020
                timedate = time.strptime(timestr, "%d %b %Y")
            except:
                try:
                    # 时间字符串格式为28/05/2020
                    timedate = time.strptime(timestr, "%d/%m/%Y")
                except:
                    try:
                        # 时间字符串格式为November 30th, 2020
                        timedate = time.strptime(timestr, "%B %dth, %Y")
                    except:
                        try:
                            # 时间字符串格式为November 1st, 2020
                            timedate = time.strptime(timestr, "%B %dst, %Y")
                        except:
                            try:
                                # 时间字符串格式为November 2nd, 2020
                                timedate = time.strptime(timestr, "%B %dnd, %Y")
                            except:
                                try:
                                    # 时间字符串格式为November 3rd, 2020
                                    timedate = time.strptime(timestr, "%B %drd, %Y")
                                except:
                                    try:
                                        # 时间字符串格式为2020-12-17
                                        timedate = time.strptime(timestr, "%Y-%m-%d")
                                    except:
                                        try:
                                            # 时间字符串格式为04 December 2020
                                            timedate = time.strptime(timestr, "%d %B %Y")
                                        except Exception as e:
                                            with open('dateeorr.txt', 'a', encoding='utf-8') as f:
                                                f.write(str(e) + '\n')
                                            return 555
        return timedate
